package spider.data.impl;

import java.util.ArrayList;
import java.util.List;

import nlp.model.Message;
import spider.common.OathPrivider;
import spider.data.DataInterface;
import weibo4j.IDs;
import weibo4j.Paging;
import weibo4j.Status;
import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.WeiboException;

public class SinaWeiboDataImpl implements DataInterface {

	Weibo weibo = OathPrivider.getSinaWeiboInstance();
	
	public List<Message> getMessageByUID(long uid) {
		List<Message> msgList=null;
		List<Status> moshengren=new ArrayList<Status>();
		User user=null;
		try {
			user = weibo.showUser(uid+"");
			int totalCount=user.getStatusesCount();
			if(totalCount>0){
				msgList=new ArrayList<Message>();
				moshengren=weibo.getUserTimeline(user.getScreenName(),new Paging(1,totalCount));
			}else{
				return null;
			}
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		Message msg=null;
		for(Status status:moshengren){
//			System.out.println(status.getText());
			boolean flag=status.getRetweeted_status()==null ? true:false;
//			System.out.println(status.getCreatedAt().getTime()+" || "+status.getText()+(flag ? "" :(" [RT] "+status.getRetweeted_status().getText())));
//			System.out.println("-----------------------------------------------------------------------------------------------------------");
//			
			msg=new Message();
			msg.setId(status.getId());
			msg.setText(status.getText());
			msg.setCreateAt(status.getCreatedAt().getTime()+"");
			if(!flag){
				Status rtStatus=status.getRetweeted_status();
				msg.setRtId(rtStatus.getId());
				msg.setRtText(rtStatus.getText());
				msg.setRtCreateAt(rtStatus.getCreatedAt()==null?"":rtStatus.getCreatedAt().getTime()+"");
			}
			msgList.add(msg);
		}
		return msgList;
	}

	public List<Long> getFriendsIdByUID(long uid) {
		
		List<Long> friendList=null;
		User user=null;
		try {
			user = weibo.showUser(uid+"");
			int totalFriends=user.getFriendsCount();
			IDs ids=weibo.getFriendsIDs(user.getScreenName(), new Paging(1,totalFriends));
			if(ids.getIDs().length==0){
				return null;
			}else{
				friendList=new ArrayList<Long>();
			}
			for(long id:ids.getIDs()){
				friendList.add(id);
			}
		}catch(WeiboException e){
			
		}
		return friendList;
	}
	
}
